//
//  ChargeViewController.m
//  云律司
//
//  Created by 韦淋 on 2017/7/3.
//  Copyright © 2017年 test. All rights reserved.
//

#import "ChargeViewController.h"

@interface ChargeViewController ()<LynnTypeButtonActionDelegate,UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UITableView *chargeTablewView;
@property (nonatomic, strong) NSMutableArray *applyingData;
@property (nonatomic, strong) NSMutableArray *confirmedData;
@property (nonatomic, strong) NSMutableArray *cancelledData;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger typeButtonIndex;
@property (nonatomic, strong) NSArray *sflxArr;
@end

@implementation ChargeViewController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = false;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.barTitleLable.text = @"收费列表 ";
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.sflxArr = @[];
    self.applyingData = [NSMutableArray array];
    self.confirmedData = [NSMutableArray array];
    self.cancelledData = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.typeButtonIndex = 0;
    [self initUI];
    [self getData];
}
- (void)loadNewData{
    [self.dataSource removeAllObjects];
    [self getData];
}
- (void)getData{
    
    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@company_id=%@",CHARGELISTS_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"cid"]] dic:nil Succeed:^(id responseData) {
        for (NSDictionary *dic  in responseData[@"result"]) {
            if ([dic[@"zt"] integerValue] == 1) {
                [self.applyingData addObject:dic];
                
            }
            if ([dic[@"zt"] integerValue] == 2) {
                [self.confirmedData addObject:dic];
            }
            if ([dic[@"zt"] integerValue] == 3) {
                [self.cancelledData addObject:dic];
            }
        }
        
        if (self.typeButtonIndex == 0) {
            self.dataSource = self.applyingData;
        }
        if (self.typeButtonIndex == 1) {
            self.dataSource = self.confirmedData;
        }
        if (self.typeButtonIndex == 2) {
            self.dataSource = self.cancelledData;
        }
        if (self.dataSource.count == 0) {
            [MBProgressHUD showSuccess:@"暂无更多数据"];
        }
        NSLog(@"chargeListData = %@",self.dataSource);
        [self.chargeTablewView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.chargeTablewView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.chargeTablewView.mj_header endRefreshing];
    }];
}
- (void)initUI{
    LynnTypeButtonView  *typeBtnView = [[LynnTypeButtonView  alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 44)];
    [typeBtnView setTypeButtonTitles:@[@"申请中",@"已确认",@"已作废"] withDownLableHeight:2 andDeleagte:self];
    [typeBtnView setTypeButtonNormalColor:[UIColor blackColor] andSelectColor:[UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f]];
    typeBtnView.backgroundColor = [UIColor whiteColor];
    [self.view insertSubview:typeBtnView atIndex:0];
    [self.view insertSubview:self.chargeTablewView belowSubview:typeBtnView];
    
}
#pragma mark - TypeButtonDelegate
//实现代理
-(void)didClickTypeButtonAction:(UIButton*)button withIndex:(NSInteger)index
{
    NSLog(@"现在点击的是%ld个",index);
    self.typeButtonIndex = index;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.applyingData removeAllObjects];
    [self.confirmedData removeAllObjects];
    [self.cancelledData removeAllObjects];
    self.dataSource = nil ;
    [self.chargeTablewView reloadData];
    [self getData];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 135;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChanrgeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chargeCell" forIndexPath:indexPath];
    cell.chargeType.text = [NSString stringWithFormat:@"收费类型： %@",self.dataSource[indexPath.row][@"sflx"]];
    cell.amountsLable.text = [NSString stringWithFormat:@"交易金额： %@",self.dataSource[indexPath.row][@"sfje"]];
    cell.tollGatherer.text = [NSString stringWithFormat:@"收费人： %@",self.dataSource[indexPath.row][@"audit_name"]];
    cell.chargeableTime.text = [NSString stringWithFormat:@"收费时间： %@",self.dataSource[indexPath.row][@"audit_time"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ChargeDetailViewController *CDVC = [[ChargeDetailViewController alloc] init];
    CDVC.listDetailDic = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:CDVC animated:YES];
}
#pragma mark - Getter

- (UITableView *)chargeTablewView{
    if (!_chargeTablewView) {
        _chargeTablewView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, self.view.bounds.size.width, self.view.bounds.size.height - 120) style:UITableViewStylePlain];
        _chargeTablewView.backgroundColor = [UIColor clearColor];
        _chargeTablewView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _chargeTablewView.delegate = self;
        _chargeTablewView.dataSource = self;
        _chargeTablewView.estimatedRowHeight = 180;
        [_chargeTablewView registerClass:[ChanrgeCell class] forCellReuseIdentifier:@"chargeCell"];
        _chargeTablewView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
    }
    return _chargeTablewView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
